package com.lori.search.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lori.netshop.util.NetshopResult;
import com.lori.netshop.util.StringUtil;
import com.lori.search.pojo.SolrResult;
import com.lori.search.service.SolrService;

@Controller
public class SolrController {
	@Autowired
	private SolrService solrServiceimp;
	
	@RequestMapping(value="/GET/solrresult",method=RequestMethod.GET)
	@ResponseBody
	public NetshopResult search(@RequestParam("q")String keyword,@RequestParam(defaultValue="1")int page,
			@RequestParam(defaultValue="20")int rows){
		//如果关键字为空则设置为默认关键字
		if(StringUtil.isEmpty(keyword)){
			keyword = "手机";
		}
		//乱码
		SolrResult search = null;
		try {
			keyword = new String(keyword.getBytes("iso-8859-1"), "utf-8");
			search = solrServiceimp.search(keyword, page, rows);
		} catch (Exception e) {
			e.printStackTrace();
			return NetshopResult.build(400, "错误",e.toString());
		}
		return NetshopResult.ok(search);
	}
}
